package configs

import (
	"encoding/json"
	"log"
	"os"
)
type Config struct {
	Mysql MySQLConfig `json:"mysql"`
	AppConfigs map[string]AppConfig `json:"appConfigs"`
}
//应用配置
type AppConfig struct {
	Addr string
}
type MySQLConfig struct {
	UserName  string `json:"userName"`
	Password  string `json:"password"`
	Host      string `json:"host""`
	Port  	  string `json:"port""`
	DbName    string `json:"dbName"`
}
type RedisConfig struct {
	UserName  string `json:"userName"`
	Password  string `json:"password"`
	Host      string `json:"host""`
	Port  	  string `json:"port""`
}
type KafkaConfig struct {
	Host      string `json:"host""`
	Port  	  string `json:"port""`
}
func LoadAppConfig(file *os.File) AppConfig {
	config := AppConfig{}
	decoder := json.NewDecoder(file)
	err := decoder.Decode(&config)
	if err != nil {
		log.Println("load config err")
		panic(err)
	}
	log.Println("load config:",config)
	return config
}
func LoadAppConfigs(file *os.File) map[string]AppConfig {
	var appConfigs map[string]AppConfig
	decoder := json.NewDecoder(file)
	err := decoder.Decode(&appConfigs)
	if err != nil {
		log.Println("load config err")
		panic(err)
	}
	log.Println("load config:",appConfigs)
	return appConfigs
}
func LoadMySQLConfig(file *os.File) MySQLConfig {
	config := MySQLConfig{}
	decoder := json.NewDecoder(file)
	err := decoder.Decode(&config)
	if err != nil {
		log.Println("load config err")
		panic(err)
	}
	log.Println("load config:",config)
	return config
}
func LoadRedisConfig(file *os.File) RedisConfig {
	config := RedisConfig{}
	decoder := json.NewDecoder(file)
	err := decoder.Decode(&config)
	if err != nil {
		log.Println("load config err")
		panic(err)
	}
	log.Println("load config:",config)
	return config
}
func LoadKafkaConfig (file *os.File) KafkaConfig{
	config := KafkaConfig{}
	decoder := json.NewDecoder(file)
	err := decoder.Decode(&config)
	if err != nil {
		log.Println("load config err")
		panic(err)
	}
	log.Println("load config:",config)
	return config
}
